##########################################################################
####	Linegraph script to graph worldwide refugee sums per year
####
#### May 2019, Elise Geissler (elisegeissler@ufl.edu)
#########################################################################

setwd("   ") 
 
if(any(grepl("package:plyr", search()))) detach("package:plyr") else message("plyr not loaded")
suppressPackageStartupMessages(library(dplyr)) 		#for manipulating data
suppressPackageStartupMessages(library(ggplot2))

#=============================================================================================================
#	Loading data
#=============================================================================================================
#=============================================================================================================

Data <- read.csv("UNHCRdata.csv", stringsAsFactors=FALSE, na.strings=c(""," ","NA"))			
head(Data)

	#renaming the column I need to make working with the data easier
colnames(Data)[6] <- "Total.Refs"
Data[is.na(Data$Total.Refs),] <- 0	#to make column sums possible

#=============================================================================================================
#	Creating refugee sums per year
#=============================================================================================================
#=============================================================================================================

RefSums <- Data %>%
		filter(Year > 1989) %>%						#filtering out years before '90
		group_by(Year) %>%						#grouping by year
		summarise (Ref.Sum = sum(Total.Refs)/1000000) %>%	#ref stock sums per year & conversion to per million
		ungroup() %>%
		data.frame()
RefSums


#=============================================================================================================
#	Graph theme
#=============================================================================================================
#=============================================================================================================

TimeSeries_Theme <-   theme (panel.background = element_rect(fill = "white",colour = "white",
                                	size = 1, linetype = "solid"),
		panel.grid.major = element_line(size = 0.5, linetype = "dotted", colour = "lightgrey"),
		axis.text.x = element_text(family = "sans", colour = "black", size = (25)),
		axis.text.y = element_text(family = "sans", colour = "black", size = (25)),
		axis.title.x=element_text(family = "sans", colour = "black", size = (28), vjust=0.2),
		axis.title.y = element_text(family = "sans", colour = "black", size = (28), vjust=8),
		plot.margin = unit(c(1,1,1,1), "cm"),
		axis.line = element_line(color="grey10"),
		plot.title = element_text(lineheight=2, face="bold", size = (32), hjust = 0.5))

#=============================================================================================================
#	Graph
#=============================================================================================================
#=============================================================================================================

Ref.plot <- ggplot(data=RefSums, aes(x=as.numeric(Year), y=Ref.Sum)) +								
       geom_line(size=1.5, show.legend = FALSE,linetype="solid", color = "steelblue4")+		
	labs(x="Years", y="Refugee population (millions)", title="World refugee population by year 1990-2016") 
Ref.plot	

Themed.Plot <- Ref.plot + TimeSeries_Theme + scale_x_continuous(breaks=seq(min(RefSums$Year),max(RefSums$Year), 5))+
			scale_y_continuous(limits = c(0, 20))
Themed.Plot 

ggsave("RefStock_Timeseries.png", Themed.Plot, width = 15, height = 10, dpi=300)





 
